package com.bluefuture.modules.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bluefuture.common.utils.PageUtils;
import com.bluefuture.common.utils.Query;
import com.bluefuture.modules.app.dao.CollectUserDao;
import com.bluefuture.modules.app.entity.CollectUserEntity;
import com.bluefuture.modules.app.service.CollectUserService;
import com.bluefuture.modules.sys.security.user.SecurityUser;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("collectUserService")
public class CollectUserServiceImpl extends ServiceImpl<CollectUserDao, CollectUserEntity> implements CollectUserService {

    @Override
    public void collect(Integer userId) {
        Long currentUserId = SecurityUser.getUserId();
        CollectUserEntity entity = new CollectUserEntity();
        entity.setUserId(currentUserId.intValue());
        entity.setCollectUserId(userId);
        save(entity);
    }

    @Override
    public void deleteCollectUser(Integer userId) {
        LambdaQueryWrapper<CollectUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectUserEntity::getUserId, SecurityUser.getUserId())
                .eq(CollectUserEntity::getCollectUserId, userId);
        List<CollectUserEntity> list = list(wrapper);
        List<Integer> ids = list.stream().map(e -> e.getId()).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(ids)) {
            for (Integer id : ids) {
                this.removeById(id);
            }
        }
    }

    @Override
    public List<Integer> listCollectUserIds() {
        LambdaQueryWrapper<CollectUserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectUserEntity::getUserId, SecurityUser.getUserId());
        List<CollectUserEntity> list = list(wrapper);
        return list.stream().map(e -> e.getCollectUserId()).collect(Collectors.toList());
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CollectUserEntity> page = this.page(
                new Query<CollectUserEntity>().getPage(params),
                new QueryWrapper<CollectUserEntity>()
        );

        return new PageUtils(page);
    }

}